import os

import requests


def testProxy():
    os.chdir(r"./data/proxy")
    url = "http://www.baidu.com"

    fp = open("host.txt","r")
    ips = fp.readlines()
    proxys = list()

    for p in ips:
        ip = p.strip("\n").split("\t")
        proxy = "http://" + ip[0] + ":"+ip[1]
        proxies = {"proxy":proxy}
        proxys.append(proxies)

    testedProxy = []
    for pro in proxys:
        try:
            s = requests.get(url,proxies=pro)
            if s.__str__() == "<Response [200]>":
                testedProxy.append(pro)
                # print(s)
        except Exception as e:
            print(e)
    return testedProxy


if __name__ == '__main__':

    testedProxy = testProxy()
    print("Testing Finished!!!")
    for pro in testedProxy:
        print(pro)

